package com.us.usws.common;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

public class CommonUtils {

	//private static Log log = LogFactory.getFactory().getInstance(CommonUtils.class);

	private static final String[] BOOLEAN_TRUE_VALUES = { "true", "y", "yes", "on", "1" };
	private static final String[] BOOLEAN_FALSE_VALUES = { "false", "n", "no", "off", "0" };

	private static final Map<String, String> MIME_MAP = new HashMap<String, String>();
	static {
		MIME_MAP.put("3gpp", "video/3gpp");
		MIME_MAP.put("3gp", "video/3gpp");
		MIME_MAP.put("mp4", "video/mp4");
		MIME_MAP.put("mpeg", "video/mpeg");
		MIME_MAP.put("mpg", "video/mpeg");
		MIME_MAP.put("mov", "video/quicktime");
		MIME_MAP.put("webm", "video/webm");
		MIME_MAP.put("flv", "video/x-flv");
		MIME_MAP.put("m4v", "video/x-m4v");
		MIME_MAP.put("mng", "video/x-mng");
		MIME_MAP.put("asx", "video/x-ms-asf");
		MIME_MAP.put("asf", "video/x-ms-asf");
		MIME_MAP.put("wmv", "video/x-ms-wmv");
		MIME_MAP.put("avi", "video/x-msvideo");
	}

	/**
	 * convert boolean to char in DB -- Y/N
	 * @param b
	 * @return
	 */
	public static String toYorN(boolean b) {
		return b ? SiteConstants.STR_BOOLEAN_YES : SiteConstants.STR_BOOLEAN_NO;
	}

	/**
	 * get boolean from string, default false
	 * @param s
	 * @return
	 */
	public static boolean toBooleanDefaultFalse(String s) {
		if (s == null) {
			return false;
		}
		for (String each : BOOLEAN_TRUE_VALUES) {
			if (s.equalsIgnoreCase(each)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * get boolean from string, default true
	 * @param s
	 * @return
	 */
	public static boolean toBooleanDefaultTrue(String s) {
		if (s == null) {
			return true;
		}
		for (String each : BOOLEAN_FALSE_VALUES) {
			if (s.equalsIgnoreCase(each)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * whether is valid mobile no
	 * @param mobileNo
	 * @return
	 */
	public static boolean isValidMobileNo(String mobileNo) {
		if (StringUtils.isBlank(mobileNo) || mobileNo.length() < 11 || mobileNo.length() > 15) {
			return false;
		}
		String last11 = mobileNo.substring(mobileNo.length() - 11, mobileNo.length());
		if (!last11.matches("^1[0-9]{10}$")) {
			return false;
		}
		if (mobileNo.length() > 11) {
			String pre = mobileNo.substring(0, mobileNo.length() - 11);
			if (!pre.equals("86") && !pre.equals("+86") && !pre.equals("+086") && !pre.equals("0086")) {
				return false;
			}
		}
		return true;
	}

	public static String getMimeType(String ext) {
		return MIME_MAP.get(ext);
	}
}
